﻿Imports System.Data.SqlClient

Public Class clsPreventivos
    Private mintIdPreventivo As Integer
    Private mintIdAutobus As Integer
    Private mstrNoEconomico As String
    Private mintIdServicio As Integer
    Private mstrNombreServicio As String
    Private mdblKmsProgramados As Double
    Private mdatFechaProgramado As Date
    Private mdatFecha As Date
    Public Property IdPreventivo() As Integer
        Get
            Return mintIdPreventivo
        End Get
        Set(ByVal value As Integer)
            mintIdPreventivo = value
        End Set
    End Property
    Public Property IdAutobus() As Integer
        Get
            Return mintIdAutobus
        End Get
        Set(ByVal value As Integer)
            mintIdAutobus = value
        End Set
    End Property
    Public Property NoEconomico() As String
        Get
            Return mstrNoEconomico
        End Get
        Set(value As String)
            mstrNoEconomico = value
        End Set
    End Property

    Public Property IdServicio() As Integer
        Get
            Return mintIdServicio
        End Get
        Set(ByVal value As Integer)
            mintIdServicio = value
        End Set
    End Property
    Public Property NombreServicio() As String
        Get
            Return mstrNombreServicio
        End Get
        Set(ByVal value As String)
            mstrNombreServicio = value
        End Set
    End Property
    Public Property KmsProgramados() As Double
        Get
            Return mdblKmsProgramados
        End Get
        Set(ByVal value As Double)
            mdblKmsProgramados = value
        End Set
    End Property
    Public Property FechaProgramado As Date
        Get
            Return mdatFechaProgramado
        End Get
        Set(ByVal value As Date)
            mdatFechaProgramado = value
        End Set
    End Property
    Public Property Fecha As Date
        Get
            Return mdatFecha
        End Get
        Set(ByVal value As Date)
            mdatFecha = value
        End Set
    End Property

    Public Sub New()
        mintIdPreventivo = 0
        mintIdAutobus = 0
        mstrNoEconomico = ""
        mintIdServicio = 0
        mstrNombreServicio = ""
        mdblKmsProgramados = 0
        mdatFechaProgramado = Now()
        mdatFecha = Now()
    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into Preventivos (" & _
                            "IdAutobus," & _
                            "NoEconomico," & _
                            "IdServicio," & _
                            "NombreServicio," & _
                            "KmsProgramados," & _
                            "FechaProgramado," & _
                            "Fecha) " & _
                            "VALUES (" & _
                            "@IdAutobus," & _
                            "substring(@NoEconomico,1,50)," & _
                            "@IdServicio," & _
                            "substring(@NombreServicio,1,200)," & _
                            "@KmsProgramados," & _
                            "@FechaProgramado," & _
                            "@Fecha) "


        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdAutobus", mintIdAutobus)
        Command.Parameters.AddWithValue("NoEconomico", mstrNoEconomico)
        Command.Parameters.AddWithValue("IdServicio", mintIdServicio)
        Command.Parameters.AddWithValue("NombreServicio", mstrNombreServicio)
        Command.Parameters.AddWithValue("KmsProgramados", mdblKmsProgramados)
        Command.Parameters.AddWithValue("FechaProgramado", mdatFechaProgramado)
        Command.Parameters.AddWithValue("Fecha", mdatFecha)

        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el preventivo ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub Actualizar()

        Dim strSqlQry As String
        Dim cnConn As New SqlConnection

        cnConn.ConnectionString = CitraConnection
        strSqlQry = "Update Preventivos Set " & _
                    "Fecha=@Fecha " & _
                    "Where IdPreventivo=@IdPreventivo"


        Dim Command As New SqlCommand(strSqlQry, cnConn)

        Command.Parameters.AddWithValue("IdPreventivo", mintIdPreventivo)
        Command.Parameters.AddWithValue("Fecha", mdatFecha)
        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar el preventivo ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If



    End Sub
End Class
